package com.example.leetcode.leetcode.hot100;

import com.example.leetcode.leetcode.list.ListNode;

import java.util.Stack;

public class Solution234 {
    public boolean isPalindrome(ListNode head) {
        Stack<ListNode> listNodes = new Stack<>();
        ListNode slow = head;
        ListNode fast = head;
        listNodes.push(slow);
        while(slow.next!=null && fast.next!=null && fast.next.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            listNodes.push(slow);
        }
        // 长度为偶数时，去掉中心值
        if(fast.next==null){
            listNodes.pop();
        }
        while (slow.next!=null){
            if(slow.next.val != listNodes.pop().val){
                return false;
            }
            slow=slow.next;
        }

        return true;
    }

    public static void main(String[] args) {
        Solution234 solution =new Solution234();
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(2);
        ListNode listNode4 = new ListNode(2);

        listNode1.ListNodesBuild(listNode1,listNode2,listNode3,listNode4);
        System.out.println(solution.isPalindrome(listNode1));
    }
}
